package com.imooc.ioc.demo1.Control;

import com.imooc.ioc.demo1.common.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class CheckCode {

    @GetMapping("/Checkcode")
    public void valicode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Object[] objs = ImageUtil.createImage();
        //将图片输出给浏览器
        BufferedImage image = (BufferedImage) objs[1];

        HttpSession session = request.getSession();

       session.setAttribute("vercode",objs[0]);

        System.out.println(session.getAttribute((String) objs[0]));

        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        try {
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
